﻿using System.Data;
using System.Data.SqlClient;

using Microsoft.ApplicationBlocks.Data;

using YouHP.IDAL;
using YouHP.Model;
using System.Collections.Generic;
using System.Text;

namespace YouHP.SqlServerDAL
{
    public class MessageDAL : IMessage
    {
        #region 添加删除基本操作

        public bool CreateMessage(MessageInfo Msg)
        {
            return SqlHelper.ExecuteNonQuery(Common.ConnStr,
                Common.MessageSP_AddMessage,
                Msg.msgfromid,
                Msg.msgfrom,
                Msg.msgtoid,
                Msg.msgto,
                Msg.message,
                Msg.PostDateTime,
                Msg.ParentId,
                Msg.IsRead) > 0;
        }

        public bool DeleteMessage(int[] idArray)
        {
            int cnt = idArray.Length;
            if (cnt <= 0)
                return false;

            StringBuilder sb = new StringBuilder();
            sb.Append("[id] in (");
            sb.Append(idArray[0]);

            for (int i = 1; i < cnt; i++)
            {
                sb.Append(", ");
                sb.Append(idArray[i]);
            }
            sb.Append(")");

            int cnt2 = SqlHelper.ExecuteNonQuery(Common.ConnStr,
                Common.MessageSP_DelMessage,
                sb.ToString());

            return cnt2 >= cnt;
        }


        public bool UpdatePmState(int uid, int IsRead)
        {
            bool succ = SqlHelper.ExecuteNonQuery(Common.ConnStr, Common.MessageSP_UpdatePmState, uid, IsRead) > 0;
            return succ;
        }

        #endregion

        #region 查找列表

        public MessageInfo GetMessage(int Pmid)
        {
            IDataReader reader = SqlHelper.ExecuteReader(Common.ConnStr,
                Common.MessageSP_GetMessageInfo,
                Pmid);
            return CBO.FillObject<MessageInfo>(reader);
        }

        public List<MessageInfo> GetMessageList(int pageSize, int pageIndex, string condstr,string orderstr, out int count)
        {
            DataSet dset = SqlHelper.ExecuteDataset(Common.ConnStr,
                Common.MessageSP_GetMessageList,
                pageSize, pageIndex, condstr, orderstr);

           IDataReader dr = dset.Tables[0].CreateDataReader();
            if (dr != null && dr.Read())
            {
                count = dr.GetInt32(0);
                dr.Close();
            }
            else
            {
                count = 0;
            }

            dr = dset.Tables[1].CreateDataReader();
            return CBO.FillCollection<MessageInfo>(dr);
        }


        public List<MessageInfo> GetMessageListByUserId(int pageSize, int pageIndex, int uid,bool isTo, out int count)
        {
            string condstr;
            if (isTo)
            {
                 condstr = string.Format(@" [msgtoid] = {0}", uid);
            }
            else
            {
                 condstr = string.Format(@" [msgfromid] = {0}", uid);
            }
            return GetMessageList(pageSize, pageIndex, condstr," [id] desc ", out count);


        }

        public List<MessageInfo> GetMsgReplyList(int pageSize, int pageIndex, int msgid,out int count)
        {

            string condstr = string.Format(@" [ParentId] = {0}", msgid);
            return GetMessageList(pageSize, pageIndex, condstr, " [id] asc ",out count);
        }

    
        public int GetNewMessageCount(int uid)
        {
            int count;
            string condstr = string.Format(@" [msgtoid] = {0} AND [isread] = {1}",uid,0);
            List<MessageInfo> lst = GetMessageList(0, 1, condstr, " [id] desc ", out count);
            return count;
        }

        #endregion
    }
}
